package cn.jiyun.controller;

import cn.jiyun.pojo.User;
import cn.jiyun.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;


    @GetMapping("findAll")
    public String findAll(Model model){
        List<User> list = userService.findAll();
        model.addAttribute("list",list);
        return "user";
    }


    //添加
    @RequestMapping("/add")
    public  String add(User user){
        userService.add(user);
        return "redirect:/findAll";
    }


    //去添加页面
    @RequestMapping("/toadd")
    public String toadd(){
        return "add";
    }

    @GetMapping("/findById")
    public String findById(String id,Model model){
        User user = userService.findById(id);
        model.addAttribute("user",user);
        return "update";
    }


    //修改
    @RequestMapping("/update")
    public String updateById(User user){
        //执行修改
        System.out.println(user);
        userService.updateById(user);
        return "redirect:/findAll";
    }


    //删除
    @GetMapping("/del")
    public String  delById(String id){
        userService.delById(id);
        return "redirect:/findAll";
    }

}
